//
//  LeftMenuView.m
//  搭建框架(代码已封装)
//
//  Created by My Mac on 2017/2/1.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "LeftMenuView.h"
#define imgWidth 16
#define Width self.frame.size.width


@interface LeftMenuView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSMutableArray * imgArr;
@property(nonatomic,strong) NSMutableArray * titleArr;


@end

@implementation LeftMenuView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initWithView];
        [self initData];
    }
    return self;
}
-(void)initData
{
    _imgArr = [[NSMutableArray alloc]initWithObjects:@"vip",@"money",@"personality",@"myCollection",@"photo",@"file", nil];
    _titleArr = [[NSMutableArray alloc]initWithObjects:@"了解会员特权",@"QQ钱包",@"个性装扮",@"我的收藏",@"我的相册",@"我的文件", nil];
}
//初始化视图
-(void)initWithView
{
    self.backgroundColor = [UIColor whiteColor];
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 160)];
    headerView.backgroundColor = [UIColor blueColor];
    
    //二维码
    UIButton * QRcode = [[UIButton alloc]initWithFrame:CGRectMake(Width - 3*imgWidth, imgWidth, 48, 48)];
    [QRcode setImage:[UIImage imageNamed:@"二维码 (1)"] forState:UIControlStateNormal];
    [headerView addSubview:QRcode];
    //二维码响应事件
    [QRcode addTarget:self action:@selector(QRcodeClick) forControlEvents:UIControlEventTouchUpInside];
    //头像信息
    UIButton * iconInfo = [[UIButton alloc]initWithFrame:CGRectMake(imgWidth, headerView.frame.size.width/3, headerView.frame.size.width-2*imgWidth, 50)];
    //头像信息的响应事件
    [iconInfo addTarget:self action:@selector(iconInfoClick) forControlEvents:UIControlEventTouchUpInside];
    //头像
    CGFloat headerImgWidth = 45;
    UIImageView * headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, headerImgWidth, headerImgWidth)];
    headerImg.layer.cornerRadius = headerImg.frame.size.width/2;
    headerImg.layer.masksToBounds = YES;
    [headerImg setImage:[UIImage imageNamed:@"headerIcon"]];
    [iconInfo addSubview:headerImg];
    
    //昵称
    UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake(headerImgWidth + 5, headerImgWidth/5, iconInfo.frame.size.width- headerImgWidth + 5, 30)];
    name.text = @"柠檬草的味道";
    [iconInfo addSubview:name];
    
    [headerView addSubview:iconInfo];
    
    //个性签名
    UIButton * personalityTitle = [[UIButton alloc]initWithFrame:CGRectMake(imgWidth+5, iconInfo.frame.origin.y+50, Width-imgWidth, 30)];
    
    UIImageView * imgPersonality = [[UIImageView alloc]initWithFrame:CGRectMake(0, 7, 16, 16)];
    imgPersonality.image = [UIImage imageNamed:@"个性签名(1)"];
    
    [personalityTitle addSubview:imgPersonality];
    
    UILabel * personalityText = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, personalityTitle.frame.size.width-18, 30)];
    personalityText.text = @"你给我时间,我给你幸福.";
    
    [personalityTitle addSubview:personalityText];
    
    //个性签名的响应事件
    [personalityTitle addTarget:self action:@selector(personalityTitleClick) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:personalityTitle];
    
    [self addSubview:headerView];
    
    
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, headerView.frame.origin.y+headerView.frame.size.height, Width, self.frame.size.height-headerView.frame.size.height-100) style:UITableViewStylePlain];
    tableView.separatorStyle = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self addSubview:tableView];
    
}
#pragma mark - 二维码的响应事件
-(void) QRcodeClick
{
    NSLog(@"我的二维码");
}

#pragma mark - 头像信息的响应事件
-(void) iconInfoClick
{
    NSLog(@"我的主页");
}
#pragma mark - 个性签名的响应事件
-(void) personalityTitleClick
{
    NSLog(@"设置个性签名");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"登录";
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *login = alertController.textFields.firstObject;
        UITextField *password = alertController.textFields.lastObject;
        NSLog(@"%@:%@",login.text,password.text);
    }];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];

    okAction.enabled = NO;
    
}
- (void)alertTextFieldDidChange:(NSNotification *)notification{
    //UIAlertController *alertController = (UIAlertController *) self.presentedViewController;
   // if (alertController) {
   //     UITextField *login = alertController.textFields.firstObject;
   //     UIAlertAction *okAction = alertController.actions.lastObject;
    //    okAction.enabled = login.text.length > 2;
   // }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.imageView.image = [UIImage imageNamed:_imgArr[indexPath.row]];
    cell.textLabel.text = _titleArr[indexPath.row];
    return cell;
}


#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
